package com.jxmcloud.business.cut.controller.app.activity;

import cn.hutool.core.util.ObjectUtil;
import com.jxmcloud.business.cut.constants.CutErrorMsg;
import com.xlcloud.platform.api.exception.BusinessException;
import com.xlcloud.platform.util.RUtil;
import lombok.extern.slf4j.Slf4j;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;
import javax.annotation.Resource;
import javax.validation.Valid;
import com.jxmcloud.business.cut.service.user.IUserManagerService;
import com.jxmcloud.business.cut.vo.user.PlayerInfoVo;
import com.jxmcloud.business.cut.util.RedissonLockUtil;
import com.jxmcloud.business.cut.constants.CutRedisConstant;
import org.springframework.beans.factory.annotation.Autowired;
import org.redisson.api.RedissonClient;
import com.jxmcloud.business.cut.service.user.IUserActivityService;


import com.jxmcloud.business.cut.service.app.activity.ITurntableService;
import com.jxmcloud.business.cut.dto.client.DrawLogListDto;
import com.jxmcloud.business.cut.vo.client.DrawLogListVo;
import com.jxmcloud.business.cut.dto.client.DrawSpecialTurntableAwardDto;
import com.jxmcloud.business.cut.vo.client.DrawSpecialTurntableAwardVo;
import com.jxmcloud.business.cut.dto.client.DrawTurntableAwardDto;
import com.jxmcloud.business.cut.vo.client.DrawTurntableAwardVo;
import com.jxmcloud.business.cut.dto.client.GetTurntableInfoDto;
import com.jxmcloud.business.cut.vo.client.GetTurntableInfoVo;


@Validated
@Slf4j
@RestController
@RequestMapping("/app/activity/TurntableController")
public class TurntableController {
    @Resource
    private IUserManagerService userManagerService;
    @Resource
    private IUserActivityService userActivityService;
    @Autowired
    private RedissonClient redissonClient;
    @Resource
    private ITurntableService turntableService;


    // 转盘抽奖日志列表
    @PostMapping("/drawLogList")
    public String drawLogList(@RequestBody @Valid DrawLogListDto dto) {
        Integer uid = 0;
        try {
            PlayerInfoVo player = userManagerService.getUserInfo(dto.getSession());
            if (ObjectUtil.isNull(player)) { throw new BusinessException(CutErrorMsg.ERROR_CODE_RELOGIN, CutErrorMsg.ERROR_RELOGIN); }
            uid = player.getBase().getUid();
            // if (userManagerService.checkPlayerLock(uid)) {throw new BusinessException("请不要连续点击!");}
            DrawLogListVo data = turntableService.drawLogList(dto, player);
            Integer[] noticeTypeList = userActivityService.getSendClientNoticeType(player);
            data.setNoticeList(noticeTypeList);
            userManagerService.refreshPlayerCache(player);
            userManagerService.removePlayerLock(uid);
            return RUtil.appSuccess(data);
        } catch (BusinessException e) {
            log.error("请求失败1: {}", e.getMessage());
            userManagerService.removePlayerLock(uid);
            return RUtil.fail2(String.valueOf(e.getCode()), e.getMessage());
        } catch (Exception e) {
            log.error("请求失败2: {}", e.getMessage());
            userManagerService.removePlayerLock(uid);
            return RUtil.fail2(String.valueOf(CutErrorMsg.ERROR_CODE_FAIL), CutErrorMsg.ERROR_FALSE);
        }
    }

    // 抽奖特殊转盘
    @PostMapping("/drawSpecialTurntableAward")
    public String drawSpecialTurntableAward(@RequestBody @Valid DrawSpecialTurntableAwardDto dto) {
        Integer uid = 0;
        try {
            PlayerInfoVo player = userManagerService.getUserInfo(dto.getSession());
            if (ObjectUtil.isNull(player)) { throw new BusinessException(CutErrorMsg.ERROR_CODE_RELOGIN, CutErrorMsg.ERROR_RELOGIN); }
            uid = player.getBase().getUid();
            // if (userManagerService.checkPlayerLock(uid)) {throw new BusinessException("请不要连续点击!");}
            DrawSpecialTurntableAwardVo data = turntableService.drawSpecialTurntableAward(dto, player);
            Integer[] noticeTypeList = userActivityService.getSendClientNoticeType(player);
            data.setNoticeList(noticeTypeList);
            userManagerService.refreshPlayerCache(player);
            userManagerService.removePlayerLock(uid);
            return RUtil.appSuccess(data);
        } catch (BusinessException e) {
            log.error("请求失败1: {}", e.getMessage());
            userManagerService.removePlayerLock(uid);
            return RUtil.fail2(String.valueOf(e.getCode()), e.getMessage());
        } catch (Exception e) {
            log.error("请求失败2: {}", e.getMessage());
            userManagerService.removePlayerLock(uid);
            return RUtil.fail2(String.valueOf(CutErrorMsg.ERROR_CODE_FAIL), CutErrorMsg.ERROR_FALSE);
        }
    }

    // 转盘抽奖
    @PostMapping("/drawTurntableAward")
    public String drawTurntableAward(@RequestBody @Valid DrawTurntableAwardDto dto) {
        Integer uid = 0;
        try {
            PlayerInfoVo player = userManagerService.getUserInfo(dto.getSession());
            if (ObjectUtil.isNull(player)) { throw new BusinessException(CutErrorMsg.ERROR_CODE_RELOGIN, CutErrorMsg.ERROR_RELOGIN); }
            uid = player.getBase().getUid();
            // if (userManagerService.checkPlayerLock(uid)) {throw new BusinessException("请不要连续点击!");}
            DrawTurntableAwardVo data = turntableService.drawTurntableAward(dto, player);
            Integer[] noticeTypeList = userActivityService.getSendClientNoticeType(player);
            data.setNoticeList(noticeTypeList);
            userManagerService.refreshPlayerCache(player);
            userManagerService.removePlayerLock(uid);
            return RUtil.appSuccess(data);
        } catch (BusinessException e) {
            log.error("请求失败1: {}", e.getMessage());
            userManagerService.removePlayerLock(uid);
            return RUtil.fail2(String.valueOf(e.getCode()), e.getMessage());
        } catch (Exception e) {
            log.error("请求失败2: {}", e.getMessage());
            userManagerService.removePlayerLock(uid);
            return RUtil.fail2(String.valueOf(CutErrorMsg.ERROR_CODE_FAIL), CutErrorMsg.ERROR_FALSE);
        }
    }

    // 获取转盘信息
    @PostMapping("/getTurntableInfo")
    public String getTurntableInfo(@RequestBody @Valid GetTurntableInfoDto dto) {
        Integer uid = 0;
        try {
            PlayerInfoVo player = userManagerService.getUserInfo(dto.getSession());
            if (ObjectUtil.isNull(player)) { throw new BusinessException(CutErrorMsg.ERROR_CODE_RELOGIN, CutErrorMsg.ERROR_RELOGIN); }
            uid = player.getBase().getUid();
            // if (userManagerService.checkPlayerLock(uid)) {throw new BusinessException("请不要连续点击!");}
            GetTurntableInfoVo data = turntableService.getTurntableInfo(dto, player);
            Integer[] noticeTypeList = userActivityService.getSendClientNoticeType(player);
            data.setNoticeList(noticeTypeList);
            userManagerService.refreshPlayerCache(player);
            userManagerService.removePlayerLock(uid);
            return RUtil.appSuccess(data);
        } catch (BusinessException e) {
            log.error("请求失败1: {}", e.getMessage());
            userManagerService.removePlayerLock(uid);
            return RUtil.fail2(String.valueOf(e.getCode()), e.getMessage());
        } catch (Exception e) {
            log.error("请求失败2: {}", e.getMessage());
            userManagerService.removePlayerLock(uid);
            return RUtil.fail2(String.valueOf(CutErrorMsg.ERROR_CODE_FAIL), CutErrorMsg.ERROR_FALSE);
        }
    }
}
